package com.jpx.jxcbiz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.common.dto.PurchaseAddDto;
import com.jpx.common.dto.PurchaseDto;
import com.jpx.common.dto.PurchaseGoodsAddDto;
import com.jpx.common.vo.ResultEntity;
import com.jpx.jxcbiz.entity.*;
import com.jpx.jxcbiz.service.*;
import com.jpx.jxcbiz.vo.PurchaseReceiptQuery;
import com.jpx.jxcbiz.vo.PurchaseTJDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *  进货控制类
 * @author 张洋洋
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/biz/purchase")
public class TPurchaseListController {

    @Autowired
    ITPurchaseListService purchaseListService;

    @Autowired
    ITPurchaseListGoodsService purchaseListGoodsService;

    @Autowired
    ITSupplierService supplierService;

    @Autowired
    ITUserService userService;

    /**
     * 客户采购统计
     * @param purchaseTJDto
     * @return
     */
    @RequestMapping("/purchaseList")
    public ResultEntity purchaseList(PurchaseTJDto purchaseTJDto){
        /**进货查询*/
        List purchaseList = purchaseListService.findByPurchase(purchaseTJDto);
        /**退货查询*/
        List returnList = purchaseListService.findByReturn(purchaseTJDto);
        List list = new ArrayList();
        list.addAll(purchaseList);
        list.addAll(returnList);
        return ResultEntity.ok(list);
    }

    /**
     * 商品分类回显
     * @return
     */
    @RequestMapping("/getGoodsType")
    public ResultEntity getGoodsType(){
        List typeList = purchaseListService.getGoodsType();
        return ResultEntity.ok(typeList);
    }

    /**
     * 进货单据查询
     * @param purchaseDto
     * @return
     */
    @RequestMapping("/purchaseQuery")
    public ResultEntity list(PurchaseDto purchaseDto){
        List<PurchaseReceiptQuery> list = purchaseListService.PurchaseReceiptQuery(purchaseDto);
        return ResultEntity.ok(list);
    }

    /**
     * 根据进货单id查询所有进货单商品
     * @param purchaseListId
     * @return
     * @throws Exception
     */
    @RequestMapping("/listGoods")
    public ResultEntity listGoods(Integer purchaseListId){
        if(purchaseListId==null){
            return null;
        }
        List<TPurchaseListGoods> purchaseListGoodsList = purchaseListGoodsService.selectGoodsById(purchaseListId);
        return ResultEntity.ok(purchaseListGoodsList);
    }

    /**
     * 根据id删除进货单信息 包括进货单里的商品
     * @param purchaseListId
     * @return
     */
    @RequestMapping("/delete")
    public ResultEntity delete(Integer purchaseListId){
        Wrapper<TPurchaseListGoods> wrapper = new EntityWrapper<>();
        wrapper.eq("purchase_list_id",purchaseListId);
        /*先删除进货单里的商品*/
        purchaseListGoodsService.delete(wrapper);
        /*再删除进货单信息*/
        purchaseListService.deleteById(purchaseListId);
        return ResultEntity.ok();
    }

    /**
     * 供应商回显
     * @return
     */
    @RequestMapping("/getSupplier")
    public ResultEntity getSupplier(){
        Wrapper<TSupplier> wrapper = new EntityWrapper<>();
        List<TSupplier> suppliers = supplierService.selectList(wrapper);
        return ResultEntity.ok(suppliers);
    }

    /**
     * 进货
     * @return
     */
    @RequestMapping("/purchaseAndWarehousing")
    public ResultEntity purchaseAndWarehousing(@RequestBody PurchaseAddDto purchaseAddDto){
        //根据用户名查找用户ID
        String username = purchaseAddDto.getUsername();
        Wrapper<TUser> wrapper = new EntityWrapper();
        wrapper.eq("user_name",username);
        TUser user = userService.selectOne(wrapper);
        //添加进货表
        TPurchaseList purchaseList = new TPurchaseList();
        BeanUtils.copyProperties(purchaseAddDto,purchaseList);
        purchaseList.setUserId(user.getId());
        purchaseListService.save(purchaseList);

        List<PurchaseGoodsAddDto> list = purchaseAddDto.getList();

        //遍历进货信息 添加到表
        for (PurchaseGoodsAddDto listGoods : list ) {
            TPurchaseListGoods purchaseListGoods = new TPurchaseListGoods();
            purchaseListGoods.setGoodsId(listGoods.getId());
            purchaseListGoods.setPurchaseListId(purchaseList.getId());
            purchaseListGoods.setCode(listGoods.getCode());
            purchaseListGoods.setModel(listGoods.getModel());
            purchaseListGoods.setName(listGoods.getName());
            purchaseListGoods.setNum(listGoods.getNum());
            purchaseListGoods.setPrice(listGoods.getSellingPrice());
            purchaseListGoods.setTotal(listGoods.getNum()*listGoods.getSellingPrice());
            purchaseListGoods.setTypeId(listGoods.getTypeId());
            purchaseListGoods.setUnit(listGoods.getUnit());
            //添加
            purchaseListGoodsService.insert(purchaseListGoods);
        }
        return ResultEntity.ok();
    }


}
